package com.ebelter.btlibrary.btble.impl;

import android.app.Activity;

import com.ebelter.btlibrary.btble.ble.BleManager;
import com.ebelter.btlibrary.btble.ble.model.BleConfigure;
import com.ebelter.btlibrary.btble.ble.model.BleDevice;
import com.ebelter.btlibrary.btble.ble.model.BleType;
import com.ebelter.btlibrary.btble.impl.buc.common.BucBleConfigure;
import com.ebelter.btlibrary.btble.impl.ecg.common.EcgBleConfigure;
import com.ebelter.btlibrary.btble.impl.ichoice.common.IChoiceBleConfigure;
import com.ebelter.btlibrary.btble.impl.ict.common.IctBleConfigure;
import com.ebelter.btlibrary.btble.impl.scale.ScaleBle;
import com.ebelter.btlibrary.btble.impl.scale.common.ScaleBleConfigure;
import com.ebelter.btlibrary.btble.impl.tmm.common.TmmBleConfigure;
import com.ebelter.btlibrary.btble.impl.wristband.common.WristbandBleConfigure;
import com.ebelter.btlibrary.util.ULog;


/**
 * 蓝牙总管理者
 * Created by Sergio Pan on 2017/12/8.
 */

public class BtBleManager {

    private static BtBleManager instance;
//    private EcgBle mEcgBle;
//    private IctBle mIctBle;
//    private BucAnalyzerBle mBucAnalyzerBle;
    private ScaleBle mScaleBle;
//    private TmmBle mTmmBle;
//    private WristbandBle mWristbandBle;
//    private IChoiceBle mIChoiceBle;
    private BleType curType;
    private BleManager curBle;
    private static Activity mActivity;

    private BtBleManager() {
    }

    public static synchronized BtBleManager getInstance(Activity aty) {
        mActivity = aty;
        if (instance == null) {
            instance = new BtBleManager();
            instance.initBleConfigure(aty);
        }
        return instance;
    }

    protected void initBleConfigure(Activity aty) {
        //TODO 初始化蓝牙设备列表

        BleType inittType = BleType.SCALE;
        initBleDevice(inittType);
        mScaleBle = ScaleBle.getInstance();



        BleManager ble = getBleManager(inittType);
        if (ble != null) {
            ble.init(aty);
            curType = inittType;
            curBle = ble;
        }
    }


    /**
     * 初始化蓝牙设备参数
     *
     * @param type
     */
    private void initBleDevice(BleType type) {
        BleConfigure configure = null;
        String[] readIds = null;
        switch (type) {
            case SCALE:  //体脂秤
                readIds = new String[]{ScaleBleConfigure.READ_ID};
                configure = new BleConfigure(ScaleBleConfigure.BLE_NAME, ScaleBleConfigure.SERVICE_ID, readIds, ScaleBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(ScaleBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(ScaleBleConfigure.DATA_HEADER_SEND);
                break;
            case ICT:  //血压计
                readIds = new String[]{IctBleConfigure.READ_ID};
                configure = new BleConfigure(IctBleConfigure.BLE_NAME, IctBleConfigure.SERVICE_ID, readIds, IctBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(IctBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(IctBleConfigure.DATA_HEADER_SEND);
                break;
            case ECG:  //心电
                readIds = new String[]{EcgBleConfigure.READ_ID, EcgBleConfigure.READ_ID_1, EcgBleConfigure.READ_ID_2};
                configure = new BleConfigure(EcgBleConfigure.BLE_NAME, EcgBleConfigure.SERVICE_ID, readIds, EcgBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(EcgBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(EcgBleConfigure.DATA_HEADER_SEND);
                break;
            case OXIMETER:  //血氧仪，ichoice
                readIds = new String[]{IChoiceBleConfigure.READ_ID, IChoiceBleConfigure.READ_ID_1, IChoiceBleConfigure.READ_ID_2, IChoiceBleConfigure.READ_ID_3};
                configure = new BleConfigure(IChoiceBleConfigure.BLE_NAME, IChoiceBleConfigure.SERVICE_ID, readIds, IChoiceBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(IChoiceBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(IChoiceBleConfigure.DATA_HEADER_SEND);
                break;
            case THERMOMETER:  //耳温枪
                readIds = new String[]{TmmBleConfigure.READ_ID};
                configure = new BleConfigure(TmmBleConfigure.BLE_NAME, TmmBleConfigure.SERVICE_ID, readIds, TmmBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(TmmBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(TmmBleConfigure.DATA_HEADER_SEND);
                break;
            case BUC_ANALYZER:  //血糖/尿酸/胆固醇分析仪
                readIds = new String[]{BucBleConfigure.READ_ID};
                configure = new BleConfigure(BucBleConfigure.BLE_NAME, BucBleConfigure.SERVICE_ID, readIds, BucBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(BucBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(BucBleConfigure.DATA_HEADER_SEND);
                break;
            case WRISTBAND:  //手环
                readIds = new String[]{WristbandBleConfigure.READ_ID};
                configure = new BleConfigure(WristbandBleConfigure.BLE_NAME, WristbandBleConfigure.SERVICE_ID, readIds, WristbandBleConfigure.WRITE_ID);
                configure.setDataHaderReceive(WristbandBleConfigure.DATA_HEADER_RECEIVE);
                configure.setDataHeaderSend(WristbandBleConfigure.DATA_HEADER_SEND);
                break;
        }

        if (configure != null) {
            ULog.i("BtBleManager", "-------initBleDevice-------configure = " + configure);
            BleDevice ble = BleDevice.getInstance();
            ble.setDeviceNames(new String[]{ScaleBleConfigure.BLE_NAME, IctBleConfigure.BLE_NAME, EcgBleConfigure.BLE_NAME, IChoiceBleConfigure.BLE_NAME,
                    TmmBleConfigure.BLE_NAME, BucBleConfigure.BLE_NAME, WristbandBleConfigure.BLE_NAME,});
            ble.setConfigure(configure);
            ble.setType(type);
        }

    }

    /**
     * 切换蓝牙设备
     */
    public synchronized BleManager switchBle(BleType type) {
        //忽略相同蓝牙设备类型的切换请求

        BleManager ble = getBleManager(type);
        if (type == curType) {
            return ble;
        }

        if (ble != null) {
            //断开当前设备蓝牙
            if (curBle != null) {
                curBle.disConnectDevice();
            }

            //重新设置蓝牙设备参数
            initBleDevice(type);

            //初始化蓝牙模块
            ble.init(mActivity);

//            //重新搜索设备
//            ble.startScan();


            //保存当前BLE设备类型
            curType = type;
            curBle = ble;
        }

        return ble;
    }


    private BleManager getBleManager(BleType type) {

        BleManager ble = null;
        switch (type) {
            case ECG:
//                ble = mEcgBle;
                break;
            case SCALE:
                ble = mScaleBle;
                break;
            case OXIMETER://血氧仪，ichoice
//                ble = mIChoiceBle;
                break;
            case WRISTBAND:
//                ble = mWristbandBle;
                break;
            case BUC_ANALYZER:
//                ble = mBucAnalyzerBle;
                break;
            case THERMOMETER:
//                ble = mTmmBle;
                break;
            case ICT:
//                ble = mIctBle;
                break;
        }
        return ble;
    }


    public void releaseOnActivityDestroy() {
        //TODO 注销，释放内存
//        mEcgBle.release();
//        mIctBle.release();
//        mIChoiceBle.release();
        mScaleBle.release();
//        mTmmBle.release();
//        mWristbandBle.release();
//        mBucAnalyzerBle.release();
//
//        mEcgBle = null;
//        mIctBle = null;
//        mIChoiceBle = null;
        mScaleBle = null;
//        mTmmBle = null;
//        mWristbandBle = null;
//        mBucAnalyzerBle = null;
//
        curBle = null;
        curType = null;
        mActivity = null;
        instance = null;
    }
}
